
const fs = window.require('fs');

const copyFile = (_src, _dst) => {
    // 创建读取流
    const readable = fs.createReadStream(_src);
    // 创建写入流
    const writable = fs.createWriteStream(_dst);
    // 通过管道来传输流
    readable.pipe(writable);
};

const copyDir = (dirPath, dstDir) => {
    const readDir = dirPath => {
        const files = fs.readdirSync(dirPath);
        files.forEach(file => {
            const stats = fs.statSync(`${dirPath}/${file}`);
            const p = `${dirPath}/${file}`;
            const dst = p.substring(p.indexOf("/") + 1, p.length);
            if (stats.isFile()) {
                copyFile(p, `${dstDir}/${dst}`);
            } else {
                if (!fs.existsSync(`${dstDir}/${dst}`)) {
                    fs.mkdirSync(`${dstDir}/${dst}`);
                }
                //将文件夹作为新的读取入口
                readDir(`${dirPath}/${file}`);
            }
        });
    };
    readDir(dirPath);
};


export { copyDir };